﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;
using Microsoft.Xna.Framework.Net;
using Microsoft.Xna.Framework.Storage;

namespace AloneLibrary
{
    public class HUD
    {
        public struct textureSprite
        {
            public string resourceName;
            public Vector2 screenPos;
            public Texture2D texture;
            public float rotation;
            public float scale;
        };

        public struct fontSprite
        {
            public string resourceName;
            public Vector2 screenPos;
            public string text;
            public float rotation;
            public float scale;
        };

        private SpriteBatch gameBatch;
        private ContentManager content;
        private SpriteFont font;
        public List<textureSprite> gameTextureSprites = new List<textureSprite>();
        public List<fontSprite> gameFontSprites = new List<fontSprite>();


        /// <summary>
        /// Initializes a new instance of the <see cref="HUD"/> class.
        /// </summary>
        /// <param name="content">The contentmanager</param>
        /// <param name="batch">The spritebatch</param>
        public HUD(ContentManager content, SpriteBatch batch)
        {
            this.content = content;
            this.gameBatch = batch;
            font = content.Load<SpriteFont>("gamefont");
        }

        /// <summary>
        /// Adds a texture sprite to the screen
        /// </summary>
        /// <param name="resourceName">Name of the resource.</param>
        /// <param name="spriteName">Name of the sprite.</param>
        /// <param name="x">The x value on the screen.</param>
        /// <param name="y">The y value on the screen.</param>
        /// <param name="rotation">The rotation of the sprite.</param>
        /// <param name="scale">The scale of the sprite.</param>
        public void addTextureSprite(string resourceName, string spriteName, float x, float y, float rotation, float scale)
        {
            textureSprite newSprite = new textureSprite();
            newSprite.resourceName = resourceName;
            newSprite.texture = content.Load<Texture2D>(@"Sprites\" + spriteName);
            newSprite.screenPos.X = x;
            newSprite.screenPos.Y = y;
            newSprite.rotation = rotation;
            newSprite.scale = scale;

            gameTextureSprites.Add(newSprite);
        }

        /// <summary>
        /// Adds a font sprite to the screen
        /// </summary>
        /// <param name="resourceName">Name of the resource.</param>
        /// <param name="spriteName">Name of the sprite.</param>
        /// <param name="x">The x value on the screen.</param>
        /// <param name="y">The y value on the screen.</param>
        /// <param name="rotation">The rotation of the sprite.</param>
        /// <param name="scale">The scale of the sprite.</param>
        public void addFontSprite(string resourceName, float x, float y, string text, float rotation, float scale)
        {
            fontSprite newSprite = new fontSprite();
            newSprite.resourceName = resourceName;
            newSprite.screenPos.X = x;
            newSprite.screenPos.Y = y;
            newSprite.text = text;
            newSprite.rotation = rotation;
            newSprite.scale = scale;

            gameFontSprites.Add(newSprite);
        }

        /// <summary>
        /// Removes the sprite.
        /// </summary>
        /// <param name="resourceName">Name of the resource.</param>
        public void removeSprite(string resourceName)
        {
            int position = 0;
            bool found = false;

            for (int i = 0; i < gameTextureSprites.Count; i++)
            {
                if (gameTextureSprites[i].resourceName.Equals(resourceName))
                {
                    found = true;
                    position = i;
                }
            }
            if (found)
                gameTextureSprites.RemoveAt(position);
        }

        /// <summary>
        /// Updates the texture sprite.
        /// </summary>
        /// <param name="resourceName">Name of the resource.</param>
        /// <param name="spriteName">Name of the sprite.</param>
        /// <param name="x">The x value on the screen.</param>
        /// <param name="y">The y value on the screen.</param>
        /// <param name="health">The health.</param>
        /// <param name="jetpack">The jetpack.</param>
        /// <param name="rotation">The rotation of the sprite.</param>
        /// <param name="scale">The scale of the sprite.</param>
        public void updateTextureSprite(string resourceName, string spriteName, float x, float y,
            string health, string jetpack, float rotation, float scale)
        {
            int position = 0;
            bool found = false;

            for (int i = 0; i < gameTextureSprites.Count; i++)
            {
                if (gameTextureSprites[i].resourceName.Equals(resourceName))
                {
                    found = true;
                    position = i;
                }
            }
            if (found)
            {
                textureSprite newSprite = new textureSprite();
                newSprite.resourceName = resourceName;
                newSprite.texture = content.Load<Texture2D>(@"Sprites\" + spriteName);
                newSprite.screenPos.X = x;
                newSprite.screenPos.Y = y;
                newSprite.rotation = rotation;
                newSprite.scale = scale;

                gameTextureSprites[position] = newSprite;
            }
        }

        /// <summary>
        /// Updates the font sprite.
        /// </summary>
        /// <param name="resourceName">Name of the resource.</param>
        /// <param name="x">The x value on the screen.</param>
        /// <param name="y">The yvalue on the screen.</param>
        /// <param name="text">The text.</param>
        /// <param name="rotation">The rotation of the sprite.</param>
        /// <param name="scale">The scale of the sprite.</param>
        public void updateFontSprite(string resourceName, float x, float y, string text, float rotation, float scale)
        {
            int position = 0;
            bool found = false;

            for (int i = 0; i < gameFontSprites.Count; i++)
            {
                if (gameFontSprites[i].resourceName.Equals(resourceName))
                {
                    found = true;
                    position = i;
                }
            }
            if (found)
            {
                fontSprite newSprite = new fontSprite();
                newSprite.resourceName = resourceName;
                newSprite.screenPos.X = x;
                newSprite.screenPos.Y = y;
                newSprite.text = text;
                newSprite.rotation = rotation;
                newSprite.scale = scale;

                gameFontSprites[position] = newSprite;
            }
        }



        /// <summary>
        /// Updates the sprite rotation.
        /// </summary>
        /// <param name="type">The type.</param>
        /// <param name="resourceName">Name of the resource.</param>
        /// <param name="spriteName">Name of the sprite.</param>
        /// <param name="rotation">The rotation of the sprite.</param>
        public void updateSpriteRotation(string type, string resourceName, string spriteName, float rotation)
        {
            int position = 0;
            bool found = false;

            if (type.Equals("Texture"))
            {
                for (int i = 0; i < gameTextureSprites.Count; i++)
                {
                    if (gameTextureSprites[i].resourceName.Equals(resourceName))
                    {
                        found = true;
                        position = i;
                    }
                }
            }
            else
            {
                for (int i = 0; i < gameFontSprites.Count; i++)
                {
                    if (gameFontSprites[i].resourceName.Equals(resourceName))
                    {
                        found = true;
                        position = i;
                    }
                }
            }
            if (found)
            {
                if (type.Equals("Texture"))
                {
                    textureSprite newSprite = new textureSprite();
                    textureSprite oldSprite = new textureSprite();
                    oldSprite = gameTextureSprites[position];

                    newSprite.resourceName = resourceName;
                    newSprite.texture = content.Load<Texture2D>(@"Sprites\" + spriteName);
                    newSprite.screenPos.X = oldSprite.screenPos.X;
                    newSprite.screenPos.Y = oldSprite.screenPos.Y;
                    newSprite.rotation = rotation;
                    newSprite.scale = oldSprite.scale;

                    gameTextureSprites[position] = newSprite;
                }
                else
                {
                    fontSprite newSprite = new fontSprite();
                    fontSprite oldSprite = new fontSprite();
                    oldSprite = gameFontSprites[position];

                    newSprite.resourceName = resourceName;
                    newSprite.screenPos.X = oldSprite.screenPos.X;
                    newSprite.screenPos.Y = oldSprite.screenPos.Y;
                    newSprite.text = oldSprite.text;
                    newSprite.rotation = rotation;
                    newSprite.scale = oldSprite.scale;

                    gameFontSprites[position] = newSprite;
                }
            }
        }

        /// <summary>
        /// Updates the font sprite.
        /// </summary>
        /// <param name="resourceName">Name of the resource.</param>
        /// <param name="spriteName">Name of the sprite.</param>
        /// <param name="text">The text to be shown on screen.</param>
        public void updateFontSprite(string resourceName, string spriteName, string text)
        {
            int position = 0;
            bool found = false;

            for (int i = 0; i < gameFontSprites.Count; i++)
            {
                if (gameFontSprites[i].resourceName.Equals(resourceName))
                {
                    found = true;
                    position = i;
                }
            }
            if (found)
            {
                fontSprite newSprite = new fontSprite();
                fontSprite oldSprite = new fontSprite();

                oldSprite = gameFontSprites[position];

                newSprite.resourceName = resourceName;
                newSprite.screenPos.X = oldSprite.screenPos.X;
                newSprite.screenPos.Y = oldSprite.screenPos.Y;
                newSprite.text = text;
                newSprite.rotation = oldSprite.rotation;
                newSprite.scale = oldSprite.scale;

                gameFontSprites[position] = newSprite;
            }

        }

        /// <summary>
        /// Draws the sprites.
        /// </summary>
        public void drawSprites()
        {
            gameBatch.Begin(SpriteBlendMode.AlphaBlend, SpriteSortMode.Deferred, SaveStateMode.SaveState);

            for (int i = 0; i < gameFontSprites.Count; i++)
            {
                gameBatch.DrawString(font, gameFontSprites[i].text, gameFontSprites[i].screenPos, Color.White,
                    gameFontSprites[i].rotation, Vector2.Zero,
                    gameFontSprites[i].scale, SpriteEffects.None, 0.0f);
            }

            for (int i = 0; i < gameTextureSprites.Count; i++)
            {
                gameBatch.Draw(gameTextureSprites[i].texture, gameTextureSprites[i].screenPos, null, Color.White, gameTextureSprites[i].rotation,
                        new Vector2(gameTextureSprites[i].texture.Width / 2, gameTextureSprites[i].texture.Height / 2),
                        gameTextureSprites[i].scale, SpriteEffects.None, 0.0f);
            }
            gameBatch.End();
        }

        /// <summary>
        /// Clears the lists.
        /// </summary>
        public void clearLists()
        {
            gameTextureSprites.Clear();
            gameFontSprites.Clear();
        }
    }
}
